package com.jcfk.eam.service.csm;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.utils.BeanUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.dao.csm.CsmBindEquipmentDao;
import com.jcfk.eam.domain.dto.csm.CsmBindEquipmentDTO;
import com.jcfk.eam.domain.po.csm.CsmBindEquipment;
import com.jcfk.eam.domain.query.csm.CsmBindEquipmentQuery;
import com.jcfk.eam.domain.uploadExcel.csm.CsmBindEquipmentUploadExcel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 耗材设备绑定
 *
 * @author yrl
 * @email
 * @date 2024-11-05
 */
@Service
public class CsmBindEquipmentService extends BaseService<CsmBindEquipmentDao, CsmBindEquipment> {

    /**
     * CsmBindEquipment分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<CsmBindEquipmentDTO> queryByPage(DataPage<CsmBindEquipmentDTO> page, CsmBindEquipmentQuery query) {
        // 分页查询
        query.setProductCode(StringUtils.isEmpty(query.getProductCode()) ? query.getProductCode() : query.getProductCode() + "%");
        query.setEquipCode(StringUtils.isEmpty(query.getEquipCode()) ? query.getEquipCode() : query.getEquipCode() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断CsmBindEquipment是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(CsmBindEquipmentDTO dto) {
        QueryWrapper<CsmBindEquipment> uniqueQuery = new QueryWrapper<>();
        uniqueQuery.eq("material_id", dto.getMaterialId());
        uniqueQuery.eq("product_code", dto.getProductCode());
        uniqueQuery.eq("equip_code", dto.getEquipCode());
        uniqueQuery.eq("equip_position", dto.getEquipPosition());
        if (!StringUtils.isEmpty(dto.getId())) uniqueQuery.ne("id", dto.getId());
        int count = this.count(uniqueQuery);

        if (count > 0) return RetResult.fail("耗材与设备绑定关系已存在");
        return RetResult.ok();
    }

    /**
     * 插入耗材设备绑定
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(CsmBindEquipmentDTO dto) {

        // 保存
        CsmBindEquipment item = new CsmBindEquipment();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新耗材设备绑定
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(CsmBindEquipmentDTO dto) {

        // 保存
        CsmBindEquipment item = new CsmBindEquipment();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除耗材设备绑定(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(CsmBindEquipmentDTO dto) {

        CsmBindEquipment item = new CsmBindEquipment();

        this.setDefaultValue(item);
        item.setDeleted(0);

        QueryWrapper<CsmBindEquipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(CsmBindEquipment item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setDeleted(0);
            item.setEnabled("1");
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取耗材设备绑定信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public CsmBindEquipmentDTO get(String id) {
        // 查询数据
        CsmBindEquipment item = this.baseMapper.selectById(id);
        CsmBindEquipmentDTO dto = new CsmBindEquipmentDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 插入耗材设备绑定
     *
     * @param list 批量插入耗材设备绑定
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertBatch(List<CsmBindEquipmentUploadExcel> list) {

        List<CsmBindEquipment> savelist = new ArrayList<>();
        list.forEach(itemOne -> {
            // 保存
            CsmBindEquipment item = new CsmBindEquipment();
            BeanUtils.copyProperties(itemOne, item);
            this.setDefaultValue(item);
            savelist.add(item);
        });
        // 保存
        if (savelist.size() > 0) super.saveBigBatch(savelist);
    }

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<CsmBindEquipmentDTO>
     */
    public List<CsmBindEquipmentDTO> getList(CsmBindEquipmentQuery query) {
        // 查询条件
        QueryWrapper<CsmBindEquipment> qw = new QueryWrapper<>();
        qw.lambda().likeRight(!StrUtil.isEmpty(query.getProductCode()), CsmBindEquipment::getProductCode, query.getProductCode());
        qw.lambda().eq(!StrUtil.isEmpty(query.getMaterialId()), CsmBindEquipment::getMaterialId, query.getMaterialId());
        qw.lambda().likeRight(!StrUtil.isEmpty(query.getEquipCode()), CsmBindEquipment::getEquipCode, query.getEquipCode());
        // 查询数据
        List<CsmBindEquipment> list = super.list(qw);

        return convertList(list, CsmBindEquipmentDTO.class);
    }

    /**
     * 根据主键获取耗材设备绑定信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        CsmBindEquipment item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }
}

